using System.Collections.Generic;
using System.Windows.Forms;
using Mbs.Modularity.AddIns;
using Mbs.Elements;
using Mbs.ComponentModel.Container;

namespace Mbs.Windows.Elements.Builders
{
    [Component(Id = "ToolStrip.ComboBox", Service = typeof(IElementBuilder))]
    public class ToolStripComboBoxBuilder : ToolStripItemBuilder<ToolStripComboBox>
    {
        private class ComboBox : ToolStripComboBox, IAddInItemHost
        {
            public string Command;
            public IAddInItem AddInItem { get; set; }

            protected override void OnKeyDown(KeyEventArgs e)
            {
                base.OnKeyDown(e);
                if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(Command))
                    Command.Publish(this, e);
            }
        }

        protected override ToolStripComboBox CreateItem(IAddInItem addInItem)
        {
            return new ComboBox { AddInItem = addInItem, Command = addInItem.Command, Name = addInItem.Path };
        }

        protected override void OnInitInstance(object caller, IAddInItem element, List<object> subItems, ToolStripItem instance)
        {
            var cmb = instance as ToolStripComboBox;
            if (cmb != null)
                BindCommand(element, cmb, "SelectedIndexChanged");
        }


    }
}
